package com.um.springboot.starter.helper;


import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.um.springboot.starter.constants.Constants;
import com.um.springboot.starter.dao.CommonDao;
import com.um.springboot.starter.properties.HshProperties;
import com.um.springboot.starter.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Component
public class SessionHelper {

    @Autowired(required = false)
    protected HttpServletRequest request;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private HshProperties hshProperties;

    @Lazy
    @Autowired
    CommonDao dao;


    public void setAttribute(String name,Object value) {
        if("redis".equals(hshProperties.getConfig().getSessionType())){
            redisTemplate.opsForHash().put("session",name,value);
        }else {
            HttpSession session = request.getSession();
            session.setAttribute(name, value);
            session.setMaxInactiveInterval(hshProperties.getConfig().getSessionTimeOut()*60);
        }
    }

    public Object getAttribute(String name) {
        if("redis".equals(hshProperties.getConfig().getSessionType())){
            return redisTemplate.opsForHash().get("session",name);
        }else {
            HttpSession session = request.getSession();
            return session.getAttribute(name);
        }
    }

    public void removeAttribute(String name){
        if("redis".equals(hshProperties.getConfig().getSessionType())) {
            redisTemplate.opsForHash().delete("session",name);
        }else{
            HttpSession session = request.getSession();
            session.removeAttribute(name);
        }
    }

    public void setSessionUser(Map user){
        setAttribute(Constants.SESSION_USER,user);
    }

    public void removeSessionUser() {
        if ("sa-token".equals(hshProperties.getConfig().getSessionType())) {
            StpUtil.getSession().delete(Constants.SESSION_USER);
        } else {
            removeAttribute(Constants.SESSION_USER);
        }
    }

    public Map getSessionUser(){
        if("token".equals(hshProperties.getConfig().getSessionType())) {
            String token = request.getHeader("token");
            if (StrUtil.isNotBlank(token)) {
                Map user = dao.queryMapBySQLName("get_user_by_token", new String[]{token});
                return user;
            }
        }else if("sa-token".equals(hshProperties.getConfig().getSessionType())) {
            String loginId = null;
            try {
                loginId = StpUtil.getLoginId(null);
            } catch (Exception ignored) {
            }
            if (StrUtil.isNotBlank(loginId)) {
                Map user = (Map) StpUtil.getSession().get(Constants.SESSION_USER);
                if (user == null) {
                    user = dao.queryMap(hshProperties.getToken().getUserTableName(), loginId);
                    if (user != null) {
                        StpUtil.getSession().set(Constants.SESSION_USER, user);
                    }
                }
                return user;
            }
        }else{
            return BeanUtil.beanToMap(getAttribute(Constants.SESSION_USER), true, true);
        }
        return null;
    }

    public String getSessionUserId(){
        if ("sa-token".equals(hshProperties.getConfig().getSessionType())) {
            try {
                return StpUtil.getLoginId(null);
            } catch (Exception ignored) {
                return null;
            }
        }

        Map user = getSessionUser();
        if (user != null) {
            return StringUtil.getTrim(user.get(hshProperties.getConfig().getSessionUserIdField()));
        }
        return null;
    }
}
